﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Xml;

namespace MetaInfoDAL
{
    public class MataInfoDAL
    {
        private XElement xDoc;
        private static XNamespace MSNS = "http://schemas.microsoft.com/office/word/2004/10/bibliography";

        public void ReadXML(string namePath)
        {
            try
            {
                // XmlReader xmlReader = new XmlTextReader(namePath);
                xDoc = XElement.Load(namePath);
            }
            catch (SystemException e)
            {
                throw new SystemException(e + ":Fehler in ReadXml - XML can not be read from " + namePath);
            }
        }

        public List<string> GetTitleList() {
            var childList = from el in xDoc.Elements(MSNS + "Source") 
                            select new { Title = (String)el.Element(MSNS + "Title") };
                                            // select (string)el;
            List<string> result = new List<string>();
            foreach(var str in childList)
            {
                result.Add((string)str.Title);
            }
            return result;
        }

        public string XgetSourceURL(string title)
        {
            IEnumerable<XElement> source = from el in xDoc.Elements(MSNS + "Source")
                                            where (string)el.Element(MSNS + "Title") == title
                                            select el;
            return source.First<XElement>().ToString();
        }

        public string GetTagWithTitle(string title)
        {
            IEnumerable<string> source = from el in xDoc.Descendants(MSNS + "Source")
                                           where (string)el.Element(MSNS + "Title") == title
                                           select (string)el.Element(MSNS +"Tag");
            return source.First<string>().ToString();
        }

        // load the BibXML string for the Bibliography with a Tag parameter.
        public string GetBibXML(String tag)
        {
            IEnumerable<XElement> bibXML = from el in xDoc.Elements(MSNS + "Source")
                                         where (string)el.Element(MSNS + "Tag") == tag
                                         select el;
            return bibXML.First<XElement>().ToString();
        }
        
        // load the Titile, Tag and SourceType from XML and return a List.
        public List<string> GetTitleTagList(ref List<MetaInfoListItem> itemList)
        {
            var childList = from el in xDoc.Elements(MSNS + "Source")
                            select new { Title = (String)el.Element(MSNS + "Title"), 
                                         Tag = (String)el.Element(MSNS+"Tag"),
                                         SourceType =(String)el.Element(MSNS +"SourceType") };
            // select (string)el;
            List<string> result = new List<string>();
            foreach (var str in childList)
            {
                string title = str.Title; 
                string tag = str.Tag;
                string srcType = str.SourceType;
                itemList.Add(new MetaInfoListItem(tag, title, srcType));
                result.Add((string)str.Title);
            }
            return result;
        }

        public string GetSourceURL(string tag)
        {
            var url = from el in xDoc.Elements(MSNS + "Source")
                      where (string)el.Element(MSNS + "Tag") == tag
                      select (string)el.Element("sourceURL");
            
            string result = url.First<string>().ToString();
            if (result == "file:/E:/docrsc/doc04.txt") return "E:/docrsc/doc04.txt";
            else if (result == "file:/E:/docrsc/doc03.txt") return "E:/docrsc/doc03.txt";
            else if (result == "file:/E:/docrsc/doc02.txt") return "E:/docrsc/doc02.txt";
            else return "E:/docrsc/doc01.txt";

        }


    }
}
